#include "bsp_handle.h"

void p_pwm0_init(void)
{
    CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
    TM_TimeBaseInitTypeDef TM_TimeBaseInitStructure;
    TM_OutputInitTypeDef TM_OutputInitStructure;
    uint32_t wCRR = 0, wPSCR = 0;

    CKCUClock.Bit.GPTM0      = 1;
    CKCUClock.Bit.AFIO       = 1;
    CKCU_PeripClockConfig(CKCUClock, ENABLE);
    /* Configure the BUZZER_GPIO_PIN as TM channel output AFIO function                                       */
    AFIO_GPxConfig(PWM_LED_R_AFIO, PWM_LED_R_PIN, AFIO_FUN_MCTM_GPTM);  /* PC4 channel0*/
    AFIO_GPxConfig(PWM_LED_B_AFIO, PWM_LED_B_PIN, AFIO_FUN_MCTM_GPTM);  /* PC5 channel1*/
    AFIO_GPxConfig(PWM_LED_W_AFIO, PWM_LED_W_PIN, AFIO_FUN_MCTM_GPTM);  /* PA6 channel2*/

    /* Compute CRR and PSCR value                                                                             */
    wCRR = (SystemCoreClock / PWM_FREQ) - 1;
    while ((wCRR / (wPSCR + 1)) > 0xFFFF)
    {
    wPSCR++;
    }
    wCRR = wCRR / (wPSCR + 1);

    /* Init BUZZER TM time-base                                                                               */
    TM_TimeBaseInitStructure.CounterReload = wCRR;
    TM_TimeBaseInitStructure.Prescaler = wPSCR;
    TM_TimeBaseInitStructure.RepetitionCounter = 0;
    TM_TimeBaseInitStructure.CounterMode = TM_CNT_MODE_UP;
    TM_TimeBaseInitStructure.PSCReloadTime = TM_PSC_RLD_IMMEDIATE;
    TM_TimeBaseInit(HT_GPTM0, &TM_TimeBaseInitStructure);
    TM_OutputInitStructure.OutputMode = TM_OM_PWM2;
//    TM_OutputInitStructure.IdleStateN = MCTM_OIS_LOW;
//    TM_OutputInitStructure.IdleState = MCTM_OIS_HIGH;
    TM_OutputInitStructure.Control = TM_CHCTL_ENABLE;
    /*init pwm duty is 33% */
    TM_OutputInitStructure.Compare = ((wCRR + 1) * (100 - 33)) / 100;

    TM_OutputInitStructure.Channel =  PWM_LED_R_TM_CHANNEL;
    TM_OutputInit(HT_GPTM0, &TM_OutputInitStructure);
    TM_OutputInitStructure.Channel =  PWM_LED_B_TM_CHANNEL;
    TM_OutputInit(HT_GPTM0, &TM_OutputInitStructure);
     TM_OutputInitStructure.Channel =  PWM_LED_W_TM_CHANNEL;
    TM_OutputInit(HT_GPTM0, &TM_OutputInitStructure);

//    pwm_channel_duty(HT_GPTM0,PWM_LED_R_TM_CHANNEL,00);
//    pwm_channel_duty(HT_GPTM0,PWM_LED_B_TM_CHANNEL,00);
//    pwm_channel_duty(HT_GPTM0,PWM_LED_W_TM_CHANNEL,00);


    TM_Cmd(HT_GPTM0, ENABLE);


}

void p_pwm0_enable(void)
{

}

void p_pwm0_duty(void)
{

}

void p_pwm0_stop(void)
{

}

void p_pwm0_disable(void)
{

}


void p_pwm1_init(void)
{

}

void p_pwm1_enable(void)
{

}

void p_pwm1_start(void)
{

}

void p_pwm1_duty(void)
{

}

void p_pwm1_stop(void)
{

}

void p_pwm1_disable(void)
{

}

